<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\Warehouse;//仓库模型


class Warehouses extends Auth
{
    //仓库的列表
    public function seting(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        if ($request->isAjax() && $request->isPost()) {
            $warehouse = new Warehouse();
            $getwarehouses = $warehouse->getsetings($res);
            echo json_encode(array('getsetings' => $getwarehouses['data'], 'size' => $getwarehouses['count']));
        } elseif ($request->isGet()) {
            return view('seting/seting');
        }
    }

    //仓库的添加
    public function setingadd(Request $request)
    {
        $data = $request->param();
        $warehouse = new warehouse();
        if ($request->isGet()) {
            return view('seting/setinginfo', [
                'str' => 'setingadd',
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getdata'])) {
            $countrys = getables('country', [], 'id,name');
            echo json_encode(array('info' => '', 'countrys' => $countrys));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'setingadd') {
            $warehouseret = $warehouse->addeditseting($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //仓库的编辑
    public function setingedit(Request $request)
    {
        $data = $request->param();
        $warehouse = new warehouse();
        if ($request->isGet()) {
            return view('seting/setinginfo', [
                'str' => 'setingedit',
                'id' => $data['id'],
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['getdata'])) {
            $countrys = getables('country', [], 'id,name');
            $info = getinfo($data['id'], 'warehouse');
            echo json_encode(array('info' => $info, 'countrys' => $countrys));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'setingedit') {
            $warehouseret = $warehouse->addeditseting($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //仓库的删除
    public function setingdel(Request $request)
    {
        $data = $request->param();
        $warehouse = new Warehouse();
        if ($request->isPost() && $request->isAjax()) {
            $warehouseret = $warehouse->delsetings($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //处理仓库的查看
    public function warehousedetail(Request $request)
    {
        $res=$request->param();
        $res['lang']=$this->lang;
        $warehouse = new Warehouse();
        if ($request->isGet()) {
            return view('seting/detail');
        }elseif ($request->isPost() && $request->isAjax() && !$res['getdata']){
            //获取请求到的数据
            $warehouseret = $warehouse->getdatastore($res);
            echo json_encode($warehouseret);
        }else if($request->isPost() && $request->isAjax() && $res['getdata']=='getdetails'){
            //仓库中详情点击
            $warehouseret = $warehouse->getdetails($res);
            echo json_encode($warehouseret);
        }else if($request->isPost() && $request->isAjax() && $res['getdata']=='getadjusts'){
            //仓库中调整点击
            $warehouseret = $warehouse->getadjusts($res);
            echo json_encode($warehouseret);
        }else if($request->isPost() && $request->isAjax() && $res['getdata']=='savedata'){
            //调整保存
            $warehouseret = $warehouse->saveadjusts($res);
            echo json_encode($warehouseret);
        }
    }

    //设置仓库的人员
    public function warseadmin(Request $request)
    {
        $warehouse = new warehouse();
        $res = $request->param();
        $lang = $this->lang;
        if ($request->isGet()) {
            //获取仓库含有的国家
            $countrys = getables('country', [], 'id,name');
            $name=getValue('warehouse',['id'=>$res['id']],'name');
            return view('seting/warseadmin', [
                'id' => $res['id'],
                'countrys' => $countrys,
                'warsename'=>$name
            ]);
        } elseif ($request->isAjax() && $request->isPost() && isset($res['getadmin'])) {
            $adminuser = array();
            if (isset($res['dempartment_id'])) {
                //获取部门下的员工
                $adminuser = getables('admin', ['department_id' => $res['dempartment_id'], 'status' => 1], 'id,nickname');
            }
            $warehouseuser = getables('warehouse2admin', ['warehouse_id' => $res['warehouse_id']]);
            //获取仓库的员工
            $adminids = array_column($warehouseuser, 'admin_id');
            //检查部门的员工是否已经存在
            foreach ($adminuser as $adminuserkey => $adminuserval) {
                if (in_array($adminuserval['id'], $adminids)) {
                    $adminuser[$adminuserkey]['is_che'] = true;
                } else {
                    $adminuser[$adminuserkey]['is_che'] = false;
                }
                //这个员工数据那个仓库
                $warehouseid = getValue('warehouse2admin', ['admin_id' => $adminuserval['id']], 'warehouse_id');
                $adminuser[$adminuserkey]['warehousename'] = getValue('warehouse', ['id' => $warehouseid], 'name');
            }
            //获取该仓库员工的职务和部门
            foreach ($warehouseuser as $warehouseuserkey => $warehouseuserval) {
                //该员工的职位和部门
                $admininfo = getinfo($warehouseuserval['admin_id'], 'admin', 'department_id,position_id');
                //员工名称
                $warehouseuser[$warehouseuserkey]['username'] = getValue('admin',['id'=>$warehouseuserval['admin_id']],'nickname');
                $departmentwhere['mkey'] = 'department_' . $admininfo['department_id'];
                //部门名称
                $warehouseuser[$warehouseuserkey]['department_name'] = getlangname('multi_lang', $departmentwhere, $lang);
                $warehouseuser[$warehouseuserkey]['department_id'] = $admininfo['department_id'];//部门id

                $positionwhere['mkey'] = 'position_' . $admininfo['position_id'];
                //职务名称
                $warehouseuser[$warehouseuserkey]['position_name'] = getlangname('multi_lang', $positionwhere, $lang);
            }
            echo json_encode(array('adminuser' => $adminuser, 'checkall' => $adminids, 'warehouseuser' => $warehouseuser));
        } elseif ($request->isAjax() && $request->isPost() && isset($res['substn'])) {
            $warehouseret = $warehouse->sunadminbtn($res);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //设置仓库的位置
    public function setingplace(Request $request)
    {
        $data = $request->param();
        $warehouse = new warehouse();
        if ($request->isGet()) {
            //显示页面
            return view('seting/setingplace', ['id' => $data['id']]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['setingplace'])) {
            //接口显示
            $resdata = getables('warehouse_place', ['warehouse_id' => $data['id']]);
            foreach ($resdata as $reskey => $resval) {
                $resdata[$reskey]['mangname'] = getinfo($resval['update_id'], 'admin')['nickname'];
                $resdata[$reskey]['update_date'] = getTimeDate($resval['update_at'], 12);
            }
            $name = getinfo($data['id'], 'warehouse')['name'];
            echo json_encode(array('resdata' => $resdata, 'name' => $name));
        } elseif ($request->isAjax() && $request->isPost() && isset($data['addplace'])) {
            //位置的添加
            $data['act'] = 'add';
            $warehouseret = $warehouse->placeadddel($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            $udata['resdata'] = isset($warehouseret['resdata']) ? $warehouseret['resdata'] : '';
            echo json_encode($udata);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['setingsub'])) {
            //位置的保存
            $data['act'] = 'sub';
            $warehouseret = $warehouse->placeadddel($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //仓库的区域设置
    public function setingarea(Request $request)
    {
        $data = $request->param();
        $warehouse = new warehouse();
        $lang = $this->lang;
        if ($request->isGet()) {
            //显示页面
            return view('seting/areaset', ['id' => $data['id']]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['areadata'])) {
            //接口显示
            $resdata = getables('warehouse_area', ['warehouse_id' => $data['id']]);
            $areatype = getlangres('area_type', $lang);
            foreach ($resdata as $reskey => $resval) {
                $resdata[$reskey]['mangname'] = getinfo($resval['update_id'], 'admin')['nickname'];
                $resdata[$reskey]['update_date'] = getTimeDate($resval['update_at'], 12);
                //位置类型
                $areatypewhere['mkey'] = 'area_type_' . $resval['type_id'];
                //获取类型说明
                $resdata[$reskey]['type_name'] = getlangname('multi_lang', $areatypewhere, $lang);;
            }
            $name = getinfo($data['id'], 'warehouse')['name'];
            echo json_encode(array('resdata' => $resdata, 'name' => $name, 'areatype' => $areatype));
        } elseif ($request->isAjax() && $request->isPost() && isset($data['areaadd'])) {
            //区域设置
            $data['act'] = 'add';
            $warehouseret = $warehouse->areaadddel($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            $udata['resdata'] = isset($warehouseret['resdata']) ? $warehouseret['resdata'] : '';
            echo json_encode($udata);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['areasub'])) {
            $data['act'] = 'sub';
            $warehouseret = $warehouse->areaadddel($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //设置仓库的距离
    public function setingroot(Request $request)
    {
        $data = $request->param();
        $warehouse = new warehouse();
        if ($request->isGet()) {
            return view('seting/setingroot', ['id' => $data['id']]);
        } elseif ($request->isAjax() && $request->isPost() && isset($data['setingroot'])) {
            $resdata = getables('warehouse_distance', ['warehouse_id' => $data['id']], 'id,warehouse_target_id,days,warehouse_id');
            //获取对应的仓库名称
            foreach ($resdata as $reskey => $resval) {
                $resdata[$reskey]['fromname'] = getinfo($resval['warehouse_id'], 'warehouse')['name'];
                $resdata[$reskey]['leavename'] = getinfo($resval['warehouse_target_id'], 'warehouse')['name'];
            }
            $maptwo['id'] = array('neq', $data['id']);
            //获取所有的仓库
            $onedata = getables('warehouse', ['id' => $data['id']], 'id,name');
            $twodata = getables('warehouse', $maptwo, 'id,name');
            echo json_encode(array('onedata' => $onedata, 'twodata' => $twodata, 'resdata' => $resdata));
        } elseif ($request->isAjax() && $request->isPost() && isset($data['setingsub'])) {
            //距离的保存
            $data['act'] = 'setingsub';
            $warehouseret = $warehouse->settingadddel($data);
            $udata["status"] = $warehouseret['status'];
            $udata['msg'] = $warehouseret['msg'];
            echo json_encode($udata);
        }
    }

    //调货单
    public function warehousetransfer(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if($request->isGet()){
            return view('warehouse/transfer');
        }elseif ($request->isAjax() && $request->isPost() && isset($res['getdata'])){
            //调货单数据的获取
            $data=$warehouse->gettransfer($res);
            echo json_encode($data);
        }
    }
    //生成调货单
    public function savetransfer(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        if ($request->isAjax() && $request->isPost()){
            $data=$warehouse->savetransfer($res);
            echo json_encode($data);
        }
    }
    //调货指令的删除
    public function deltransfer(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if ($request->isPost() && $request->isAjax()){
            $data = $warehouse->deltransfer($res);
            echo json_encode($data);
        }
    }
    //调货单删除
    public function deltransferorder(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if ($request->isPost() && $request->isAjax()){
            $data = $warehouse->deltransfer($res);
            echo json_encode($data);
        }
    }

    //查看调货单
    public function transdetail(Request $request){
        $res = $request->param();
        $res['lang']= $this->lang;
        $warehouse = new warehouse();
        if($request->isGet()){
            return view('warehouse/orderview',[
                'id'=>$res['id'],
                'warehouse_id'=>$res['warehouse_id'],
            ]);
        }else if ($request->isAjax() && $request->isPost()){
            $data=$warehouse->transdetail($res);
            echo json_encode($data);
        }
    }
    //货物调出
    public function warehousetakeout(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if ($request->isGet()){
            return view('warehouse/takeout');
        }else if($request->isPost() && $request->isAjax() && isset($res['getdata'])){
            $data=$warehouse->takeoutarraival($res);
            echo json_encode($data);
        }
    }
    //确认出库点击
    public function takeoutorder(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if($request->isAjax() && $request->isPost() && isset($res['getdata'])){
            $data=$warehouse->gettakeoutorder($res,1);
            echo json_encode($data);
        }elseif ($request->isAjax() && $request->isPost() && isset($res['savedata'])){
            $data=$warehouse->savetakedata($res);
            echo json_encode($data);
        }
    }
    //货物调出中的撤消
    public function revoketakeout(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if($request->isPost() && $request->isAjax()){
            $data=$warehouse->arrivaltakeoutrevoke($res,1);
            echo json_encode($data);
        }
    }
    //收货入库
    public function warehousearrival(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if ($request->isGet()){
            return view('warehouse/arrival');
        }else if($request->isPost() && $request->isAjax() && isset($res['getdata'])){
            $data=$warehouse->takeoutarraival($res);
            echo json_encode($data);
        }
    }
    //确认收到点击
    public function arrivalorder(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if($request->isAjax() && $request->isPost() && isset($res['getdata'])){
            $data=$warehouse->gettakeoutorder($res,2);
            echo json_encode($data);
        }elseif ($request->isAjax() && $request->isPost() && isset($res['savedata'])){
            $data=$warehouse->savearrivaldata($res);
            echo json_encode($data);
        }
    }
    //收货入库撤消
    public function annulorder(Request $request){
        $res = $request->param();
        $warehouse = new warehouse();
        $res['lang']= $this->lang;
        if($request->isAjax() && $request->isPost()){
            $data=$warehouse->arrivaltakeoutrevoke($res,2);
            echo json_encode($data);
        }
    }
}